/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huaweicloud.rds.sdk.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.rds.v3.RdsClient;
import com.huaweicloud.sdk.rds.v3.model.BackupPolicy;
import com.huaweicloud.sdk.rds.v3.model.ListBackupsRequest;
import com.huaweicloud.sdk.rds.v3.model.ListBackupsResponse;
import com.huaweicloud.sdk.rds.v3.model.SetBackupPolicyRequest;
import com.huaweicloud.sdk.rds.v3.model.SetBackupPolicyRequestBody;
import com.huaweicloud.sdk.rds.v3.model.SetBackupPolicyResponse;
import com.huaweicloud.sdk.rds.v3.model.ShowBackupPolicyRequest;
import com.huaweicloud.sdk.rds.v3.model.ShowBackupPolicyResponse;
import com.huaweicloud.sdk.rds.v3.region.RdsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * rds备份相关示例代码
 *
 * @since 2022-10-17
 */
public class BackupDemo {
    private static final Logger logger = LoggerFactory.getLogger(BackupDemo.class.getName());

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String instanceId = "<YOUR INSTANCE_ID>";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        RdsClient client = RdsClient.newBuilder()
                .withCredential(auth)
                .withRegion(RdsRegion.CN_EAST_2)
                .build();

        // 查看自动备份策略
        showBackupPolicy(client, instanceId);

        // 设置自动备份策略
        setBackupPolicy(client, instanceId);

        // 查询备份列表 要根据实际设置的时间 实际生成备份文件之后才可以看到对应的备份文件
        listBackups(client, instanceId);
    }

    private static void showBackupPolicy(RdsClient client, String instanceId) {
        ShowBackupPolicyRequest request = new ShowBackupPolicyRequest();
        request.withInstanceId(instanceId);
        try {
            ShowBackupPolicyResponse response = client.showBackupPolicy(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static void setBackupPolicy(RdsClient client, String instanceId) {
        SetBackupPolicyRequest request = new SetBackupPolicyRequest();
        request.withInstanceId(instanceId);
        SetBackupPolicyRequestBody body = new SetBackupPolicyRequestBody();
        BackupPolicy backupPolicyBody = new BackupPolicy();
        backupPolicyBody.withKeepDays(7) // your keepDays
                .withStartTime("02:00-03:00") // your startTime
                .withPeriod("1,2"); // your period
        body.withBackupPolicy(backupPolicyBody);
        request.withBody(body);
        try {
            SetBackupPolicyResponse response = client.setBackupPolicy(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static void listBackups(RdsClient client, String instanceId) {
        ListBackupsRequest request = new ListBackupsRequest();
        request.withInstanceId(instanceId);
        try {
            ListBackupsResponse response = client.listBackups(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

}
